﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Korhaz
{
    public partial class Recept : Form
    {
        Betegek b;
        Orvosok o;
        GyogyszerKeszlet gy;
        private bool Err;
        private string ErrMsg;
       
        //private System.Drawing.Printing.PrintDocument printDocument1;
        
        //private System.Drawing.Printing.PrintDocument myDocument1 = null;
        //private System.IO.StringReader myStringReader = null;
        private System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
       
        public Recept()
        {
            InitializeComponent();
            
            Err = false;
            b = new Betegek(ref Err);
            if (Err)
            {
                MessageBox.Show("Az objektumot nem lehet letrehozni");
            };
        }

 

        private void button1_Click(object sender, EventArgs e)
        {
            Beteg b = new Beteg();
            b.Show();
        }

        private void Betoltes()
        {
            string ErrMsg = "";
            SqlDataReader reader = b.BetegLista(ref ErrMsg);
            listView1.Items.Clear();
            if (ErrMsg == "OK")
            {
                while (reader.Read())
                {
                    ListViewItem item = new ListViewItem(reader[0].ToString());
                    item.SubItems.Add(reader[1].ToString());
                    item.SubItems.Add(reader[2].ToString());
                    item.SubItems.Add(reader[3].ToString());
                    item.SubItems.Add(reader[4].ToString());
                    listView1.Items.Add(item);
                }
                reader.Close();
            }
            else
            {
                {
                    this.Close();
                }
            }
        }

        private void Betoltess()
        {
            string ErrMsg = "";
            SqlDataReader reader = o.OrvosLista(ref ErrMsg);
            listView2.Items.Clear();
            if (ErrMsg == "OK")
            {
                while (reader.Read())
                {
                    ListViewItem item = new ListViewItem(reader[0].ToString());
                    item.SubItems.Add(reader[1].ToString());
                    item.SubItems.Add(reader[2].ToString());
                    item.SubItems.Add(reader[3].ToString());
                    item.SubItems.Add(reader[4].ToString());
                    item.SubItems.Add(reader[5].ToString());
                    item.SubItems.Add(reader[6].ToString());
                    item.SubItems.Add(reader[7].ToString());
                    listView2.Items.Add(item);
                }
                reader.Close();
            }
        }
            private void Betoltesss()
        {
            string ErrMsg = "";
            SqlDataReader reader = gy.GyogyszerLista(ref ErrMsg);
            listView4.Items.Clear();
            if (ErrMsg == "OK")
            {
                while (reader.Read())
                {
                    ListViewItem item = new ListViewItem(reader[0].ToString());
                    item.SubItems.Add(reader[1].ToString());
                    item.SubItems.Add(reader[2].ToString());
                    item.SubItems.Add(reader[3].ToString());
                    listView4.Items.Add(item);
                }
                reader.Close();
            }
        }

        private void Recept_Load(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
            
            o = new Orvosok(ref Err);
            b = new Betegek(ref Err);
            gy = new GyogyszerKeszlet(ref Err);
            this.Betoltes();
            this.Betoltess();
            this.Betoltesss();
            txtAfa.Text = "19";
            txtAfa.Enabled = false;
            txtAfaErtek.Enabled = false;
            txtAr.Enabled = false;
            txtBrutto.Enabled = false;
            txtNetto.Enabled = false;
            txtNev.Enabled = false;
            //txtReceptID.Enabled = false;
            txtGyogyID.Enabled = false;
            int imaxkod = gy.MaxKod(ref ErrMsg);
            txtsorszam.Text = Convert.ToString(imaxkod + 1);
            txtsorszam.Enabled = false;
            txtMennyiseg.Enabled = false;
            txtMaradek.Enabled = false;
        }

        private void butBetoltes_Click(object sender, EventArgs e)
        {
            ListViewItem newItem = new ListViewItem();
            newItem = listView1.SelectedItems[0];
           // txtID.Text = newItem.SubItems[0].Text;
            txtBetegNev.Text = newItem.SubItems[1].Text;
            txtBetegCim.Text = newItem.SubItems[2].Text;
            txtBetegTel.Text = newItem.SubItems[3].Text;
            txtBetegSzSz.Text = newItem.SubItems[4].Text;
        }

        private void butBetoltes1_Click(object sender, EventArgs e)
        {
            ListViewItem newItem = new ListViewItem();
            newItem = listView2.SelectedItems[0];
            // txtID.Text = newItem.SubItems[0].Text;
            txtOrvosNev.Text = newItem.SubItems[1].Text;  
            txtOrvosTel.Text = newItem.SubItems[3].Text;      
        }

        

        private void butBetolt_Click(object sender, EventArgs e)
        {
            ListViewItem newItem = new ListViewItem();
            newItem = listView4.SelectedItems[0];
            txtGyogyID.Text = newItem.SubItems[0].Text;
            txtNev.Text = newItem.SubItems[1].Text;
            txtMennyiseg.Text = newItem.SubItems[2].Text;
            txtAr.Text = newItem.SubItems[3].Text;
        }

        private void butSzamol_Click(object sender, EventArgs e)
        {
            int mennyi = Convert.ToInt32(txtMenny.Text);
            int ar = Convert.ToInt32(txtAr.Text);
            int netto = mennyi * ar;
            txtNetto.Text = Convert.ToString(netto);
            int afa = Convert.ToInt32(txtAfa.Text);
            double afaertek = (netto * 0.19);
            txtAfaErtek.Text = Convert.ToString(afaertek);
            double brutto = netto + afaertek;
            txtBrutto.Text = Convert.ToString(brutto);
            
            ListViewItem newItem = new ListViewItem();
            newItem = listView4.SelectedItems[0];
            txtMennyiseg.Text = newItem.SubItems[2].Text;
            int a = Convert.ToInt32(txtMennyiseg.Text);
            int b = Convert.ToInt32(txtMenny.Text);
            int c = a - b;
            txtMaradek.Text = Convert.ToString(c);
           
        }

        private void Hozzaadd()
        {
            string ErrMsg = "";
            SqlDataReader reader = gy.ReceptLista(ref ErrMsg);
            listView3.Items.Clear();
            if (ErrMsg == "OK")
            {
                while (reader.Read())
                {
                    ListViewItem item = new ListViewItem(reader[0].ToString());
                    item.SubItems.Add(reader[1].ToString());
                    item.SubItems.Add(reader[2].ToString());
                    item.SubItems.Add(reader[3].ToString());
                    item.SubItems.Add(reader[4].ToString());
                    item.SubItems.Add(reader[5].ToString());
                    item.SubItems.Add(reader[6].ToString());
                    item.SubItems.Add(reader[7].ToString());
                    item.SubItems.Add(reader[8].ToString());
                    listView3.Items.Add(item);
                }
                reader.Close();
            }
        }

        private void butAdd_Click(object sender, EventArgs e)
        {
            
            gy.InsertRecept(txtsorszam.Text, txtGyogyID.Text, txtNev.Text, txtMenny.Text, txtAr.Text, txtNetto.Text, txtAfa.Text, txtAfaErtek.Text, txtBrutto.Text);
            this.Hozzaadd();
           // gy.UpdateGyogy(txtGyogyID.Text, txtNev.Text, txtMaradek.Text, txtAr.Text);
           
            txtGyogyID.Clear();
            txtNev.Clear();
            txtMenny.Clear();
            txtAr.Clear();
            txtNetto.Clear();
            txtAfaErtek.Clear();
            txtBrutto.Clear();
            txtMaradek.Clear();
            txtMennyiseg.Clear(); 
        }
         
        private void butKilepes_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void NyomtatasiElonezet()
        {
            try
            {
                //Bitmap panelImage = new Bitmap(tabPage1.Width(), tabPage1.Height());
                //tabPage1.DrawToBitmap(panelImage, tabPage1.ClientRectangle);
            }
            catch (Exception exp)
            {
                System.Console.WriteLine(exp.Message.ToString());
            }
        }
        private void butElonezet_Click(object sender, EventArgs e)
        {
            //printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintPage);
            //string str = "STRING";
            //myStringReader = new System.IO.StringReader(str);
            //PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
            //printPreviewDialog1.Document = this.printDocument2;
            //printPreviewDialog1.ShowDialog();
            //    try
            //    {
            //        PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
            //        printPreviewDialog1.Document = this.printDocument1;
            //        printPreviewDialog1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
            //        printPreviewDialog1.SetBounds(20, 20, this.Width, this.Height);
            //        printPreviewDialog1.ShowDialog();
            //    }
            //    catch (Exception exp)
            //    {
            //        System.Console.WriteLine(exp.Message.ToString());
            //    }

            Recept r = new Recept();
            r.TopLevel = false;
            r.Parent = tabControl1.TabPages[0];
            r.FormBorderStyle = FormBorderStyle.Fixed3D;
            r.Dock = DockStyle.Fill;


            printPreviewDialog1.Document = doc;
            printPreviewDialog1.Show();
        }

        //private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        //{
        //    this.Focus();
            
        //}

     
        float SzamolBrutto()
        {
            double sum = 0;
            try
            {
                for (int i = 0; i < listView3.Items.Count; i++)
                {
                    if (listView3.Items[i].SubItems.Count == 9)
                    {
                        try
                        {
                            double partialsum = Convert.ToDouble(listView3.Items[i].SubItems[8].Text);
                            sum += partialsum;
                            listView3.Items[i].SubItems[8].Text = partialsum.ToString("#,###.00");
                        }
                        catch (FormatException ex)
                        {
                            sum += 0;
                        }
                    }
                }
                
                txtOsszeg.Text = sum.ToString("#,###.00");
                if (this.txtAfa.Text.Length == 0)
                    txtNettoOsszeg.Text = sum.ToString("#,###.00");
                else
                    txtNettoOsszeg.Text = (sum + sum * Convert.ToDouble(this.txtAfa.Text)/100).ToString("#,###.00");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

            return (float) sum;
        }
        float SzamolNetto()
        {
            double sum = 0;
            try
            {
                for (int i = 0; i < listView3.Items.Count; i++)
                {
                    if (listView3.Items[i].SubItems.Count == 9)
                    {
                        try
                        {
                            double partialsum = Convert.ToDouble(listView3.Items[i].SubItems[5].Text);
                            sum += partialsum;
                            listView3.Items[i].SubItems[5].Text = partialsum.ToString("#,###.00");
                        }
                        catch (FormatException ex)
                        {
                            sum += 0;
                        }
                    }
                }

                txtNettoOsszeg.Text = sum.ToString("#,###.00");
                //if (this.txtAfa1.Text.Length == 0)
                //    txtNettoOsszeg.Text = sum.ToString("#,###.00");
                //else
                //    txtNettoOsszeg.Text = (sum + sum *1).ToString("#,###.00");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

            return (float)sum;
        }
        float SzamolAfa()
        {
            double sum = 0;
            try
            {
                for (int i = 0; i < listView3.Items.Count; i++)
                {
                    if (listView3.Items[i].SubItems.Count == 9)
                    {
                        try
                        {
                            double partialsum = Convert.ToDouble(listView3.Items[i].SubItems[7].Text);
                            sum += partialsum;
                            listView3.Items[i].SubItems[7].Text = partialsum.ToString("#,###.00");
                        }
                        catch (FormatException ex)
                        {
                            sum += 0;
                        }
                    }
                }

                txtAfa1.Text = sum.ToString("#,###.00");
                //if (this.txtAfa.Text.Length == 0)
                //    txtNettoOsszeg.Text = sum.ToString("#,###.00");
                //else
                //    txtNettoOsszeg.Text = (sum + sum * Convert.ToDouble(this.txtAfa.Text) / 100).ToString("#,###.00");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

            return (float)sum;
        }
        private void butSzamol1_Click(object sender, EventArgs e)
        {
            SzamolBrutto();
            SzamolNetto();
            SzamolAfa();
            if (radioButton1.Checked == true)
                label16.Visible = true;
            else
                label16.Visible = false;
        }

        private void butNyomtatas_Click(object sender, EventArgs e)
        {
            printDialog1.Document = this.printDocument1;
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
            }
        }

        private void txtKereses_TextChanged(object sender, EventArgs e)
        {
            if (txtKereses.Text != "")
            {
                listView1.Items.Clear();
                string ErrMsg = "";
                String fL = txtKereses.Text[0].ToString();
                SqlDataReader reader = b.ElsoBetegLista(ref ErrMsg, fL);

                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        listView1.Items.Add(item);
                    }
                }
            }
            else
            {
                b = new Betegek(ref Err);
                string ErrMsg = "";
                SqlDataReader reader = b.BetegLista(ref ErrMsg);

                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        listView1.Items.Add(item);
                    }
                }
            }
        }

        private void txtKeresesOrvos_TextChanged(object sender, EventArgs e)
        {
            if (txtKeresesOrvos.Text != "")
            {
                listView2.Items.Clear();
                string ErrMsg = "";
                String fL = txtKeresesOrvos.Text[0].ToString();
                SqlDataReader reader = o.ElsoOrvosLista(ref ErrMsg, fL);

                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[5].ToString());
                        item.SubItems.Add(reader[6].ToString());
                        item.SubItems.Add(reader[7].ToString());
                        listView2.Items.Add(item);
                    }
                }
            }
            else
            {
                o = new Orvosok(ref Err);
                string ErrMsg = "";
                SqlDataReader reader = o.OrvosLista(ref ErrMsg);

                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[5].ToString());
                        item.SubItems.Add(reader[6].ToString());
                        item.SubItems.Add(reader[7].ToString());
                        listView2.Items.Add(item);
                    }
                }
            }
        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
            printPreviewDialog1.Document = this.printDocument1;
            printPreviewDialog1.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }
        public void lementes()
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = "PDF files (*.pdf)|*.pdf|All files(*.*)|*.*";

            if (sf.ShowDialog() == DialogResult.OK)
            {
                System.IO.FileStream fs = (System.IO.FileStream)sf.OpenFile();
                Bitmap panelImage = new Bitmap(tabPage1.Width, tabPage1.Height);
                tabPage1.DrawToBitmap(panelImage, tabPage1.ClientRectangle);
                //switch (sf.FilterIndex)
                //{
                //    case 1:
                //        panelImage.Save(fs, ImageFormat.Jpeg);
                //        break;

                //    case 2:
                //        panelImage.Save(fs, ImageFormat.Bmp);
                //        break;
                //}
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lementes();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "PDF Files (*.pdf)|*.pdf";

            if (of.ShowDialog() == DialogResult.OK)
            {
            }
        }

        private void butFrissit_Click(object sender, EventArgs e)
        {
             gy.frissit(txtGyogyID.Text, txtNev.Text, txtMennyiseg.Text, txtAr.Text);
             this.Betoltesss();
            //SqlConnection conn;
            //conn = new SqlConnection("Data Source=TIMI-PC\\SQLEXPRESS;Initial Catalog=Korhaz2;Integrated Security=SSPI");
            //try
            //{
            //    conn.Open();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("Nem sikerult kapcsolodni az adatbazishoz: " + ex.Message, "Hiba", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}

            //string q = "SELECT Mennyiseg FROM GyogyszerKeszlet WHERE GyogyID= '" + gyogyID.ToString() + "'";
            //string error = string.Empty;
            //SqlDataReader reader = ExecuteReader(q, ref error);
            //if (error == "OK")
            //{
            //    int x = Convert.ToInt32(paramterValues[3]);
            //    int osszeg = 0;
            //    while (reader.Read())
            //    {
            //        osszeg = x - Convert.ToInt32(reader[0].ToString());
            //    }
            //    reader.Close();

            //    string q2 = "SELECT Mennyiseg FROM GyogyszerKeszlet WHERE GyogyID= '" + gyogyID.ToString() + "'";
            //    string errorno = string.Empty;
            //    SqlDataReader reader2 = ExecuteReader(q, ref error);

            //    int x2 = 0;
            //    while (reader2.Read())
            //    {
            //        x2 = Convert.ToInt32(reader2[0].ToString());
            //    }
            //    reader2.Close();

            //    DialogResult konkurencia = new DialogResult();
            //    konkurencia = MessageBox.Show("1. Pessimista konkurencia (No) \n 2. Optimista konkurencia (Yes)", "Konkurenciakezeles valasztasa", MessageBoxButtons.YesNo);
            //    if (konkurencia == DialogResult.No)
            //    {
            //        SqlCommand comm = conn.CreateCommand();
            //        SqlTransaction trans = conn.BeginTransaction();
            //        comm.Connection = conn;
            //        comm.Transaction = trans;
            //        comm.Parameters.Add("@Mennyiseg", SqlDbType.Int).Value = mennyiseg.ToString();
            //        comm.CommandText = "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE";
            //        comm.ExecuteNonQuery();
            //        comm.CommandText = "UPDATE GyogyszerKeszlet SET Mennyiseg = @Mennyiseg WHERE GyogyID= " + gyogyID.ToString();
            //        comm.ExecuteNonQuery();

            //        if (osszeg >= 0)
            //        {
            //            System.Threading.Thread.Sleep(2000);
            //            trans.Commit();
            //            MessageBox.Show("Sikerult a modositas, az ertek " + x + " lett.");
            //        }
            //        else
            //        {
            //            MessageBox.Show("Nem sikerult a modositas, az ertek " + x + " lesz.");
            //        }
            //    }
            //    if (konkurencia == DialogResult.Yes)
            //    {
            //        string q2a = "SELECT Mennyiseg FROM GyogyszerKeszlet WHERE GyogyID= '" + gyogyID.ToString() + "'";
            //        string error2 = string.Empty();

            //        SqlDataReader reader3 = ExecuteReader(q2a, ref error2);
            //        int x3 = 0;
            //        while (reader3.Read())
            //        {
            //            x3 = Convert.ToInt32(reader3[0].ToString());
            //        }
            //        reader3.Close();
            //        if (error2 == "OK")
            //        {
            //            if (x != x3)
            //            {
            //                DialogResult dr = new DialogResult();
            //                dr = MessageBox.Show("1. Marad a regi (No) \n 2. Jovahagyom a mostani modositast (Yes) \n 3. Marad a masike: " + x3 + "(Cancel)", "Optimistakonkurencia kezeles", MessageBoxButtons.YesNoCancel);
            //                if (dr == DialogResult.No)
            //                {
            //                    parameterNames[3] = "@Mennyiseg";
            //                    parameterValues[3] = x2.ToString();

            //                    MessageBox.Show("Az ertek a regi " + x2.ToString() + " maradt.");
            //                    return ExecuteStoredProcedureNonQuery("szamla", parameterNames, parameterValues);
            //                }
            //                if (dr == DialogResult.Cancel)
            //                {
            //                    string q2Cancel = "SELECT Mennyiseg FROM GyogyszerKeszlet WHERE GyogyID= '" + gyogyID.ToString() + "'";
            //                    string error2Cancel = string.Empty;
            //                    SqlDataReader readerCancel = ExecuteReader(q2Cancel, ref error2Cancel);
            //                    int x4 = 0;
            //                    while (readerCancel.Read())
            //                    {
            //                        x4 = Convert.ToInt32(readerCancel[0].ToString());
            //                    }
            //                    readerCancel.Close();
            //                    parameterNames[3] = "@Mennyiseg";
            //                    parameterValues[3] = x4.ToString();

            //                    MessageBox.Show("Az uj ertek " + x4.ToString() + " lett.");
            //                    return ExecuteStoredProcedureNonQuery("szamla", parameterNames, parameterValues);
            //                }
            //            }
            //            else
            //            {
            //                MessageBox.Show("Sikeres modositas, " + x + " db van a keszletben.");
            //            }
            //        }
            //        else
            //        {
            //            MessageBox.Show("Sikertelen modositas.");
            //        }
            //    }
            //}
            //return null;
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
      
    }
}
